package stancel.peter.smartsilencer;

import java.util.List;

import android.content.Context;
import android.graphics.Color;
import android.graphics.Typeface;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;

/**
 * Vytvara vzhlad prvkov nachadzajucich sa v liste triedy MainActivity.
 * @author Peter Stancel
 */
public class CustomAdapter extends ArrayAdapter<Event> {

	private List<Event> listOfEvents;
	private LayoutInflater inflater;
	private View customView;
	
	/**
	 * Konstruktor.
	 * @param context Context
	 * @param listOfEvents list udalosti
	 */
	public CustomAdapter(Context context, List<Event> listOfEvents) {
		super(context, 0, listOfEvents);
		this.listOfEvents = listOfEvents;
		this.inflater = LayoutInflater.from(context);
	}

	@Override
	public int getCount() {
		return listOfEvents.size();
	}

	@Override
	public Event getItem(int position) {
		return listOfEvents.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	/**
	 * Vytvara vzhlad jednej polozky listu udalosti.
	 */
	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		Event event = listOfEvents.get(position);
		customView = inflater.inflate(R.layout.custom_adapter, null);
		
		customView.setBackgroundResource(R.color.white);

		TextView tv = (TextView) customView.findViewById(R.id.text_name);
		tv.setText(event.getName());

		tv = (TextView) customView.findViewById(R.id.text_mode);
		if (event.isVibration()) {
			tv.setText(R.string.vibration_mode);
		} else
			tv.setText(R.string.silence_mode);

		tv = (TextView) customView.findViewById(R.id.turn_on_off);
		if (event.isTurnOn()) {
			tv.setText(R.string.on);
			tv.setTextColor(Color.GREEN);
		} else {
			tv.setText(R.string.off);
			tv.setTextColor(Color.RED);
		}
		
		//ImageView imageView = (ImageView) customView.findViewById(R.id.image_event);
		//imageView.setImageResource(R.drawable.clock);
		setTimeText(event);
		setDaysText(event);

		return customView;
	}
	
	/**
	 * Vytvara text od kedy a do kedy je udalost aktivna.
	 * @param eventCalendar udalost
	 */
	private void setTimeText(Event eventCalendar){
		TextView tv = (TextView) customView.findViewById(R.id.text_time);
		tv.setVisibility(View.VISIBLE);
				
		StringBuilder string = new StringBuilder(tv.getText() + " " + eventCalendar.getDateStart() / 100 + ":" );
		
		int startMinutes = eventCalendar.getDateStart() % 100;
		if(startMinutes < 10){
			string.append("0");
		}
		string.append(startMinutes + " - " + eventCalendar.getDateEnd() / 100 + ":");
		
		int endMinutes = eventCalendar.getDateEnd() % 100;
		if(endMinutes < 10){
			string.append("0");
		}
		string.append(endMinutes);
		
		tv.setText(string);
	}
	
	/**
	 * Vytvara text, v ktore dni je udalost aktivna.
	 * @param eventTime udalost
	 */
	private void setDaysText(Event eventTime){
		TextView tvSun = (TextView) customView.findViewById(R.id.text_sunday);
		TextView tvMon = (TextView) customView.findViewById(R.id.text_monday);
		TextView tvTue = (TextView) customView.findViewById(R.id.text_tuesday);
		TextView tvWed = (TextView) customView.findViewById(R.id.text_wednesday);
		TextView tvThu = (TextView) customView.findViewById(R.id.text_thursday);
		TextView tvFri = (TextView) customView.findViewById(R.id.text_friday);
		TextView tvSat = (TextView) customView.findViewById(R.id.text_saturday);

		tvSun.setVisibility(View.VISIBLE);
		tvMon.setVisibility(View.VISIBLE);
		tvTue.setVisibility(View.VISIBLE);
		tvWed.setVisibility(View.VISIBLE);
		tvThu.setVisibility(View.VISIBLE);
		tvFri.setVisibility(View.VISIBLE);
		tvSat.setVisibility(View.VISIBLE);
				
		if (eventTime.getDays()[0]) {
			changeTypeface(tvSun);
		}

		if (eventTime.getDays()[1]) {
			changeTypeface(tvMon);
		}

		if (eventTime.getDays()[2]) {
			changeTypeface(tvTue);
		}

		if (eventTime.getDays()[3]) {
			changeTypeface(tvWed);
		}

		if (eventTime.getDays()[4]) {
			changeTypeface(tvThu);
		}

		if (eventTime.getDays()[5]) {
			changeTypeface(tvFri);
		}

		if (eventTime.getDays()[6]) {
			changeTypeface(tvSat);
		}
	}
	
	/**
	 * Zmeni typ textu.
	 * @param tv textView
	 */
	private void changeTypeface(TextView tv) {
		tv.setTypeface(Typeface.DEFAULT_BOLD);
		tv.setTextColor(Color.BLACK);
	}
}